#pragma once

#include <functional>
#include <string>
#include "InetAddr.hpp"
#include "Log.hpp"

#define ListenConnection 0
#define NormalConnection 1

class Reactor;
class Connection;

using namespace log_ns;
using handler_t = std::function<void(Connection*)>;



class Connection
{
public:
    Connection(int sockfd) : _sockfd(sockfd)
    {}
    void RegistHandler(handler_t handler_recver,handler_t handler_sender, handler_t handler_excepter)
    {
        _handler_recver = handler_recver;
        _handler_sender = handler_sender;
        _handler_excepter = handler_excepter;
    }
    void SetType(int type)
    {
        _type = type;
    }
    int Type()
    {
        return _type;
    }
    void SetEvents(uint32_t events)
    {
        _events = events;
    }
    uint32_t Events()
    {
        return _events;
    }
    int Sockfd()
    {
        return _sockfd;
    }
    std::string& Inbuffer()
    {
        return _inbuffer;
    }
    std::string& Outbuffer()
    {
        return _outbuffer;
    }
    void DiscardOutbuffer(int n)
    {
        _outbuffer.erase(0,n);
    }
    void Appendinbuffer(const std::string& s)
    {
        _inbuffer+=s;
    }
    void AppendOutbuffer(const std::string& s)
    {
        _outbuffer+=s;
    }
    void Close()
    {
        ::close(_sockfd);
    }

    ~Connection()
    {}
private:
    int _sockfd;
    uint32_t _events;
    std::string _inbuffer;
    std::string _outbuffer;
    int _type;

public:
    handler_t _handler_recver;
    handler_t _handler_sender;
    handler_t _handler_excepter;
    InetAddr _addr;
    Reactor* _R;
};